package com.dhwl.hsh.merchant.presenter;

import android.util.Log;
import android.widget.Toast;

import com.dhwl.hsh.lib.bean.HttpResponse;
import com.dhwl.hsh.lib.bean.resp.DataPage;
import com.dhwl.hsh.lib.bean.resp.OrderListResponse;
import com.dhwl.hsh.lib.constant.ServerConstant;
import com.dhwl.hsh.lib.core.HshException;
import com.dhwl.hsh.lib.core.mvp.BasePresenter;
import com.dhwl.hsh.lib.utils.ToastUtils;
import com.dhwl.hsh.lib.utils.logger.Logger;
import com.dhwl.hsh.merchant.HshApplication;
import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.model.impl.OrderModel;
import com.dhwl.hsh.merchant.presenter.iview.IGetOrdersView;
import com.dhwl.hsh.merchant.ui.activity.BaseActivity;
import com.dhwl.hsh.merchant.ui.fragment.BaseFragment;
import com.dhwl.hsh.merchant.ui.fragment.OrderListFragment;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * 我的订单列表
 *
 * @author Derek.Kidd
 * @version V1.0.0
 * @date 2016-04-18 18:48
 */
public class OrderPresenter extends BasePresenter<IGetOrdersView> {


    public OrderPresenter() {

    }

    public void getOrders(final int pageNo, int orderState) {

        Subscriber<HttpResponse<DataPage<OrderListResponse>>> subscriber = new Subscriber<HttpResponse<DataPage<OrderListResponse>>>() {

            @Override
            public void onCompleted() {
                if (OrderPresenter.this.mCompositeSubscription != null)
                    OrderPresenter.this.mCompositeSubscription.remove(this);
            }

            @Override
            public void onError(Throwable e) {
                Logger.e("getOrders" + (null != e ? e.getMessage() : "e == null "));
                if (pageNo == ServerConstant.LIST_START_PAGE) {
                    ((BaseActivity) ((OrderListFragment) OrderPresenter.this.getMvpView()).getActivity()).hideWaitingDialog();
                    ((OrderListFragment) OrderPresenter.this.getMvpView()).showNetErrorPage();
                }
                ToastUtils.show(HshApplication.mContext, R.string.network_not_exist, Toast.LENGTH_SHORT);
            }

            @Override
            public void onNext(HttpResponse<DataPage<OrderListResponse>> listHttpResponse) {
                DataPage<OrderListResponse> responseBody = listHttpResponse.getData();

                List<OrderListResponse> orderListResponses = responseBody.getListData();
                orderListResponses = null == orderListResponses ? new ArrayList<OrderListResponse>() : orderListResponses;
                if (listHttpResponse.getStatus() != 0) {
                    HshException exception = new HshException(listHttpResponse.getStatus(), listHttpResponse.getDescribe());
                    OrderPresenter.this.getMvpView().onGetOrdersFailure(exception);
                } else {
                    if (pageNo == 1 && (null == orderListResponses || orderListResponses.size() == 0)) {
                        ((OrderListFragment) OrderPresenter.this.getMvpView()).showEmptyPage();
                    } else {
                        ((OrderListFragment) OrderPresenter.this.getMvpView()).showContentPage();
                    }
                    OrderPresenter.this.getMvpView().onGetOrdersSuccess(orderListResponses);
                }
            }
        };
        this.mCompositeSubscription.add(OrderModel.getInstance().getOrders(pageNo, orderState).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                if (pageNo == ServerConstant.LIST_START_PAGE)
                    ((BaseActivity) ((OrderListFragment) OrderPresenter.this.getMvpView()).getActivity()).showWaitingDialog();

            }
        }).doAfterTerminate(new Action0() {
            @Override
            public void call() {
                if (pageNo == ServerConstant.LIST_START_PAGE)
                    ((BaseActivity) ((OrderListFragment) OrderPresenter.this.getMvpView()).getActivity()).hideWaitingDialog();
            }
        }).subscribe(subscriber));
    }

    public void cancelOrder(int type, String orderId) {
        Subscriber<HttpResponse<String>> subscriber = new Subscriber<HttpResponse<String>>() {

            @Override
            public void onCompleted() {
                if (OrderPresenter.this.mCompositeSubscription != null)
                    OrderPresenter.this.mCompositeSubscription.remove(this);
            }

            @Override
            public void onError(Throwable e) {
                Log.e("getOrders", e.getMessage());
                ToastUtils.show(HshApplication.mContext, R.string.network_not_exist, Toast.LENGTH_SHORT);
            }

            @Override
            public void onNext(HttpResponse<String> orderDetailRespHttpResponse) {
                if (orderDetailRespHttpResponse.getStatus() != 0) {
                    HshException exception = new HshException(orderDetailRespHttpResponse.getStatus(), orderDetailRespHttpResponse.getDescribe());
                    OrderPresenter.this.getMvpView().onCancelOrderFalure(exception);
                } else {
                    ToastUtils.show(HshApplication.mContext, "取消订单成功", Toast.LENGTH_SHORT);
                    OrderPresenter.this.getMvpView().onCancelOrderSuc();
                }
            }
        };
        this.mCompositeSubscription.add(OrderModel.getInstance().cancelOrder(type, orderId).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                ((BaseActivity) ((BaseFragment) OrderPresenter.this.getMvpView()).getActivity()).showWaitingDialog();
            }
        }).doAfterTerminate(new Action0() {
            @Override
            public void call() {
                ((BaseActivity) ((BaseFragment) OrderPresenter.this.getMvpView()).getActivity()).hideWaitingDialog();
            }
        }).subscribe(subscriber));
    }

}
